﻿Imports System.Data.SqlClient

Public Class CategoriaDAO

    Public Function listar() As Collection
        Dim lista As New Collection

        Try
            Dim cn As New SqlConnection(ConexionBD.obtenerCadenaConexion())
            cn.Open()

            Dim sql = "SELECT ID_CATEGORIA, NOMBRE, DESCRIPCION FROM CATEGORIA"
            Dim cmd As New SqlCommand(sql, cn)

            Dim dr As SqlDataReader
            dr = cmd.ExecuteReader()
            While dr.Read
                Dim modelo As New Categoria
                modelo.idCategoria = dr(0).ToString
                modelo.nombre = dr(1).ToString
                modelo.descripcion = dr(2).ToString
                lista.Add(modelo)
            End While

            dr.Close()
            cn.Close()

        Catch ex As SqlException
            Throw New DAOExcepcion(ex.ToString)
        End Try

        Return lista
    End Function

    Public Function insertar(ByVal modelo As Categoria) As Categoria
        Try
            Dim cn As New SqlConnection(ConexionBD.obtenerCadenaConexion())
            cn.Open()

            Dim sql = "INSERT INTO CATEGORIA (NOMBRE, DESCRIPCION) VALUES (@VALOR1, @VALOR2)"

            Dim cmd As New SqlCommand(sql, cn)
            cmd.Parameters.AddWithValue("@VALOR1", modelo.nombre)
            cmd.Parameters.AddWithValue("@VALOR2", modelo.descripcion)

            Dim afectados = cmd.ExecuteNonQuery

            If afectados <> 1 Then
                Throw New DAOExcepcion("CategoriaDAO: No insertó")
            End If

            ' LEER EL ULTIMO ID
            sql = "SELECT @@IDENTITY"
            cmd = New SqlCommand(sql, cn)
            Dim dr As SqlDataReader
            dr = cmd.ExecuteReader()
            If dr.Read Then
                modelo.idCategoria = CType(dr(0), Integer)
            End If

            dr.Close()
            cn.Close()

        Catch ex As Exception
            Throw New DAOExcepcion(ex.ToString)
        End Try

        Return modelo

    End Function

    Public Sub actualizar(ByVal modelo As Categoria)
        Try
            Dim cn As New SqlConnection(ConexionBD.obtenerCadenaConexion())
            cn.Open()

            Dim sql = "UPDATE CATEGORIA SET NOMBRE=@VALOR1, DESCRIPCION=@VALOR2 WHERE ID_CATEGORIA=@VALOR3"

            Dim cmd As New SqlCommand(sql, cn)
            cmd.Parameters.AddWithValue("@VALOR1", modelo.nombre)
            cmd.Parameters.AddWithValue("@VALOR2", modelo.descripcion)
            cmd.Parameters.AddWithValue("@VALOR3", modelo.idCategoria)

            Dim afectados = cmd.ExecuteNonQuery

            If afectados <> 1 Then
                Throw New DAOExcepcion("CategoriaDAO: No actualizó")
            End If

            cn.Close()

        Catch ex As Exception
            MsgBox(ex.Message)
            Throw New DAOExcepcion(ex.ToString)
        End Try
    End Sub

    Public Sub eliminar(ByVal idCategoria As Integer)
        Try
            Dim cn As New SqlConnection(ConexionBD.obtenerCadenaConexion())
            cn.Open()

            Dim sql = "DELETE FROM CATEGORIA WHERE ID_CATEGORIA=@VALOR1"

            Dim cmd As New SqlCommand(sql, cn)
            cmd.Parameters.AddWithValue("@VALOR1", idCategoria)

            Dim afectados = cmd.ExecuteNonQuery

            If afectados <> 1 Then
                Throw New DAOExcepcion("CategoriaDAO: No eliminó")
            End If

            cn.Close()

        Catch ex As Exception
            Throw New DAOExcepcion(ex.ToString)
        End Try
    End Sub

    Public Function obtener(ByVal idCategoria As Integer) As Categoria
        Dim modelo As New Categoria

        Try
            Dim cn As New SqlConnection(ConexionBD.obtenerCadenaConexion())
            cn.Open()

            Dim sql = "SELECT ID_CATEGORIA, NOMBRE, DESCRIPCION FROM CATEGORIA WHERE ID_CATEGORIA=@VALOR1"
            Dim cmd As New SqlCommand(sql, cn)

            cmd.Parameters.AddWithValue("@VALOR1", idCategoria)

            Dim dr As SqlDataReader
            dr = cmd.ExecuteReader()
            If dr.Read Then
                modelo.idCategoria = dr(0).ToString
                modelo.nombre = dr(1).ToString
                modelo.descripcion = dr(2).ToString
            End If

            dr.Close()
            cn.Close()

        Catch ex As SqlException
            Throw New DAOExcepcion(ex.ToString)
        End Try

        Return modelo
    End Function

    Public Function buscarPorNombre(ByVal nombre As String) As Collection
        Dim lista As New Collection

        Try
            Dim cn As New SqlConnection(ConexionBD.obtenerCadenaConexion())
            cn.Open()

            Dim sql = "SELECT ID_CATEGORIA, NOMBRE, DESCRIPCION FROM CATEGORIA WHERE UPPER(NOMBRE) LIKE @VALOR1"
            Dim cmd As New SqlCommand(sql, cn)
            cmd.Parameters.AddWithValue("@VALOR1", "%" + nombre.ToUpper + "%")

            Dim dr As SqlDataReader
            dr = cmd.ExecuteReader()

            While dr.Read
                Dim modelo As New Categoria
                modelo.idCategoria = dr(0).ToString
                modelo.nombre = dr(1).ToString
                modelo.descripcion = dr(2).ToString
                lista.Add(modelo)
            End While

            dr.Close()
            cn.Close()

        Catch ex As SqlException
            Throw New DAOExcepcion(ex.ToString)
        End Try

        Return lista
    End Function


    Public Function listarproducto() As Collection


        Dim listaProducto As New Collection

        Try
            Dim cn As New SqlConnection(ConexionBD.obtenerCadenaConexion())
            cn.Open()

            Dim sql = "SELECT IDCATEGORIA, NOMBRE, DESCRIPCION FROM CATEGORIA"
            Dim cmd As New SqlCommand(Sql, cn)

            Dim dr As SqlDataReader
            dr = cmd.ExecuteReader()
            While dr.Read
                Dim modelo As New Categoria
                modelo.idCategoria = dr(0).ToString
                modelo.nombre = dr(1).ToString
                modelo.descripcion = dr(2).ToString
                listaProducto.Add(modelo)
            End While

            dr.Close()
            cn.Close()

        Catch ex As SqlException
            Throw New DAOExcepcion(ex.ToString)
        End Try
        Return listaProducto

    End Function
End Class

